package com.luckmgr.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.constant.LuckmgrConstant;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityService;


@Controller
@RequestMapping(value ="/activity")
public class ActivityController {
	
	@Autowired
	ActivityService activityServiceImpl;
	
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,String name,Integer status,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", name);
		PageBean<Activity> pagebean = activityServiceImpl.selectListPage(map, page);
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("name", name);
		return "activity/index";
	}
	
	
	
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		Activity activity = null;
		if(null != id && 0 != id){
			Activity temactivity = activityServiceImpl.selectByPrimaryKey(id);
			if(null == temactivity || temactivity.getStatus()==1){
				model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
				return "activity/addup";
			}
			activity = activityServiceImpl.selectByPrimaryKey(id);
		}
		if(activity!=null){
			model.addAttribute("entity", activity);
		}
		return "activity/addup";
	}

	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			Activity activity = activityServiceImpl.selectByPrimaryKey(id);
			if(null ==activity || 1 == activity.getStatus()){
				model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
				return "activity/addup";
			}
			activityServiceImpl.deleteActivity(id);
		}
		return "redirect:/activity";
	}

	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,@ModelAttribute("entity") Activity activity) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == activity.getStartTime() || null == activity.getEndTime()){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "activity/addup";
		}
		
		activity.setOperatorName(user.getUserName());
		activity.setOperatorIp(user.getLoginIp());
		
		if(activity.getId()!=null && 0!=activity.getId()){
			Activity temactivity = activityServiceImpl.selectByPrimaryKey(activity.getId());
			if(null == temactivity || temactivity.getStatus()==1){
				model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
				return "activity/addup";
			}
			activityServiceImpl.updateEntity(activity);
		}else{
			
			if(StringUtils.isNull(activity.getName()) || StringUtils.isNull(activity.getSeriesCode()) ){
				model.addAttribute("msg", "数据不完整请重新录入！");
				return "activity/addup";
			}
			
			
			activityServiceImpl.addEntity(activity);
		}
		return "redirect:/activity";
	}
	
	
	
	@RequestMapping(value = "instart")
	public String instart(Model model,Integer id) {
		if(0==id){
			model.addAttribute("msg", "系统错误");
			return "activity/instart";
		}
		if(null != id && 0 != id){
			Activity activity = activityServiceImpl.selectByPrimaryKey(id);
			if(activity!=null){
				model.addAttribute("id", id);
				model.addAttribute("name", activity.getName());
				model.addAttribute("status", activity.getStatus());
				model.addAttribute("mapstatus",LuckmgrConstant.ACTIVITY_STATUS);
			}
		}
		return "activity/instart";
	}
	
	@RequestMapping(value = "upstatus")
	public String upstatus(Model model,Integer status,Integer id) {
		if(0==id){
			model.addAttribute("msg", "系统错误");
			return "activity/instart";
		}
		int rint = activityServiceImpl.updateStatus(id, status);
		if(rint == 1){
			return "redirect:/activity";
		}else{
			model.addAttribute("msg", "该系列的活动还有进行中！不能开启新活动！");
			return "activity/instart";
		}
		
	}
	
	
}
